package com.lottery.service.pc28.wealthService.impl;

import com.alibaba.fastjson.JSON;
import com.lottery.dao.pc28.wealthDao.AccountDao;
import com.lottery.dao.pc28.wealthDao.AccountTransactionDao;
import com.lottery.dao.pc28.wealthDao.BankcardDao;
import com.lottery.dto.AccountTransactionDto;
import com.lottery.dto.ResultDto;
import com.lottery.entity.Account;
import com.lottery.entity.AccountTransaction;
import com.lottery.entity.Bankcard;
import com.lottery.service.pc28.wealthService.WithdrawalsService;
import com.lottery.utils.RandCodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class WithdrawalsServiceImpl implements WithdrawalsService {
	@Autowired
	private AccountDao accountdao;

	@Autowired
	private BankcardDao bankcardDao;

	@Resource
	public AccountTransactionDao accountTransactionDao;

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 根据用户user_id查询余额和银行卡的name
	 */

	public String selectWithdhall(int userId) {
		Map<String, Object> maxMap = new HashMap<>();
		ResultDto<Double> result = new ResultDto<Double>();
		try {
			Account account = accountdao.selectAccountByUserId(userId);
			double account_funds = account.getAccountFunds();
			List<Object> list = new ArrayList<Object>();
			List<Bankcard> bankcardlist = bankcardDao.selectBankcardByAccountId(account.getId());
			if (bankcardlist.isEmpty()) {
				maxMap.put("result", "1");
				maxMap.put("msg", "当前用户没绑定银行卡");

				return JSON.toJSONString(maxMap, true);
			}
			for (Bankcard bankcard : bankcardlist) {
				Map<String, Object> map = new HashMap<String, Object>();
				String bankcardname = bankcard.getBankcardName();
				String bankcard_number = bankcard.getBankcardNumber();
				String number = bankcard_number.substring(bankcard_number.length() - 4, bankcard_number.length());
				String str = bankcardname + "(" + number + ")";
				map.put("bankname", str);
				map.put("bankcardNumber", bankcard_number);
				list.add(map);
			}
			if (list.size() > 0) {
				maxMap.put("array", list);
				maxMap.put("result", "0");
				maxMap.put("msg", "200");
				maxMap.put("data", account_funds);
			} else {
				maxMap.put("result", "2");
				maxMap.put("msg", "400");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("selectWithdhall error:", e);
			maxMap.put("result", "3");
			maxMap.put("msg", e.toString());
		}
		return JSON.toJSONString(maxMap, true);
	}

	/**
	 * 插入充值都得数据 user_id 用户id transaction_money 变动金额 transaction_type 变动类型 money_go
	 * 去向来源
	 * 
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public ResultDto<String> insertWithdrawDemo(int userId, double transaction_money, int transaction_type,
			String money_go) throws Exception {
		ResultDto<String> resultDto = new ResultDto<String>();
		try {
			Account account = accountdao.selectAccountByUserId(userId);
			double account_funds = account.getAccountFunds();
			if (account_funds < transaction_money) {
				resultDto.setData("余额不足");
				resultDto.setSuccess(false);
				return resultDto;
			}
			// account.setAccountFunds(account_funds - transaction_money);
			account.setAccountFunds(transaction_money);
			int updateNum = accountdao.reduceAccount(account);
			if (updateNum == 0) {
				resultDto.setData("余额不足");
				resultDto.setSuccess(false);
				return resultDto;
			}

			int account_id = account.getId();
			AccountTransaction accountTransaction = new AccountTransaction();
			accountTransaction.setAccountId(account_id);
			accountTransaction.setTransactionMoney(transaction_money);
			accountTransaction.setTransactionType(transaction_type);
			accountTransaction.setMoneyGo(money_go);
			accountTransaction.setMoney(transaction_money);
			accountTransaction.setTransactionDate(new Date());
			accountTransaction.setState(1);
			// accountdao.updateAccount(account);
			accountTransaction.setOrderId(RandCodeUtil.getOrderId());
			accountTransactionDao.insertAccountTransaction(accountTransaction);
			resultDto.setData("提现成功");
			resultDto.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("selectWithdhall error:", e);
			resultDto.setSuccess(false);
			resultDto.setData("提现失败");
			throw new Exception("insertWithdrawDemo error");
		}
		return resultDto;
	}

	/**
	 * 插入账户流水
	 * 
	 * @return
	 */
	@Override
	public ResultDto<AccountTransactionDto> insertAccountTransactionDto(AccountTransactionDto accountTransactionDto) {
		ResultDto<AccountTransactionDto> resultDto = new ResultDto<AccountTransactionDto>();
		int userId = accountTransactionDto.getUserId();
		Account account = accountdao.selectAccountByUserId(userId);
		if (account == null) {
			resultDto.setMessage("账户不存在");
			resultDto.setSuccess(false);
			return resultDto;
		}
		int id = account.getId();
		try {
			accountTransactionDto.setAccountId(id);
			AccountTransaction accounttransaction = new AccountTransaction();
			accounttransaction.setExpect(accountTransactionDto.getExpect());
			accounttransaction.setAccountId(accountTransactionDto.getAccountId());
			accounttransaction.setBettingId(accountTransactionDto.getBettingId());
			accounttransaction.setFee(accountTransactionDto.getFee());
			accounttransaction.setMoneyGo(accountTransactionDto.getMoneyGo());
			accounttransaction.setMoneySources(accountTransactionDto.getMoneySources());
			accounttransaction.setRemarks(accountTransactionDto.getRemarks());
			accounttransaction.setTransactionDate(new Date());
			accounttransaction.setTransactionMoney(accountTransactionDto.getTransactionMoney());
			accounttransaction.setTransactionType(accountTransactionDto.getTransactionType());
			accounttransaction.setState(accountTransactionDto.getState());
			accounttransaction.setOrderId(accountTransactionDto.getOrderId());
			accountTransactionDao.insertAccountTransaction(accounttransaction);
			resultDto.setMessage("添加成功");
			resultDto.setSuccess(true);
		} catch (Exception e) {
			resultDto.setMessage("添加失败");
			resultDto.setSuccess(false);
			logger.error("insertAccountTransaction:", e);
		}
		return resultDto;
	}

}
